//
// Created by alexa on 5/21/2024.
//

#include "MissilesService.h"

void MissilesService::setUnitsService(UnitsService *unitsService) {
    this->unitsService = unitsService;
}

Missile *MissilesService::getById(string id) {
    Missile *missile = repository.findById(id);
    if (!missile) {
        throw EntityNotFound("Missile with this id wasn't found.");
    }
    return missile;
}

vector<Missile *> MissilesService::getAll() {
    return repository.findAll();
}

void MissilesService::saveMissile(Missile *missile) {
    repository.save(missile);
}

Missile *
MissilesService::createOne(std::string name, int calibre, float length, float weight, float flightRange, float impactArea,
                           float speed) {
    Missile *missile = new Missile(name, calibre, length, weight, flightRange, impactArea, speed);
    repository.save(missile);
    return missile;
}

void MissilesService::deleteOne(string id) {
    Missile *missile = getById(id);
    CombatUnit *unit = unitsService->findByMissileId(id);
    if (unit) {
        unit->setMissileId("");
        unitsService->saveUnit(unit);
    }
    repository.deleteOne(missile);
}
